package com.jianan.mis.app.basedata;

import static com.jianan.mis.util.MD5.MD5Encode;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.domain.Employee;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.service.EmployeeService;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.SpringBeanFactory;
import com.jianan.mis.util.StringUtils;

public class ChangePasswordView extends ViewPart {

	private Text txtConfirmNewPassword;
	private Text txtNewPassword;
	private Text txtOldPassword;
	public static final String ID = "com.jianan.mis.app.basedata.ChangePasswordView"; //$NON-NLS-1$

	private EmployeeService employeeService = (EmployeeService) SpringBeanFactory.getBean("employeeService");
	private Employee currentEmployee;
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label.setText("输入原密码:");

		txtOldPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
		final GridData gd_txtOldPassword = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtOldPassword.setLayoutData(gd_txtOldPassword);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_1.setText("输入新密码:");

		txtNewPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
		final GridData gd_txtNewPassword = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtNewPassword.setLayoutData(gd_txtNewPassword);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("确认新密码:");

		txtConfirmNewPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
		final GridData gd_txtConfirmNewPassword = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtConfirmNewPassword.setLayoutData(gd_txtConfirmNewPassword);

		final Button btnSave = new Button(container, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (StringUtils.isEmpty(txtOldPassword.getText())) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "请输入原密码!");
					txtOldPassword.setFocus();
					return;
				}
				if (!currentEmployee.getPassword().equals(MD5Encode(txtOldPassword.getText()))) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "原密码输入不正确，请重新输入!");
					txtOldPassword.setFocus();
					return;
				}
				if (StringUtils.isEmpty(txtNewPassword.getText())) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "请输入新密码!");
					txtNewPassword.setFocus();
					return;
				}
				if (txtNewPassword.getText().length() > 10) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "密码长度不得超过10位，请重新输入!");
					txtNewPassword.setText("");
					txtNewPassword.setFocus();
					return;
				}
				if (StringUtils.isEmpty(txtConfirmNewPassword.getText())) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "请确认新密码!");
					txtConfirmNewPassword.setFocus();
					return;
				}
				if (!txtNewPassword.getText().equals(txtConfirmNewPassword.getText())) {
					MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
							IMessageKeys.INFORMATION, "新密码输入不一致，请重新输入!");
					txtConfirmNewPassword.setText("");
					txtConfirmNewPassword.setFocus();
					return;
				}
				currentEmployee.setPassword(txtNewPassword.getText());
				employeeService.update(currentEmployee);

				MessageDialog.openInformation(e.display.getActiveShell(), 
						IMessageKeys.SUCCESS, "修改密码成功!");
				reset();
			}
		});
		btnSave.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		btnSave.setText("确认");
		btnSave.setImage(ImageManager.getImage(IImageKeys.OK));

		final Button btnReset = new Button(container, SWT.NONE);
		btnReset.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				reset();
			}
		});
		btnReset.setText("重置");
		btnReset.setImage(ImageManager.getImage(IImageKeys.RESET));

		currentEmployee = employeeService.findByName(Session.getInstance().getEmployeeName());
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	private void reset() {
		txtOldPassword.setText("");
		txtNewPassword.setText("");
		txtConfirmNewPassword.setText("");
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
